package com.zzw.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

/**
 * @author Administrator
 */
@Aspect
public class RequestLogAop {

    private static final Logger logger = LoggerFactory.getLogger(RequestLogAop.class);

    private final ApplicationContext applicationContext;

    public RequestLogAop(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Pointcut("@annotation(com.zzw.aop.RequestLog)")
    public void logBefore() {
    }

    @Before("logBefore()")
    public void before(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature method = (MethodSignature) signature;
        RequestLog requestLog = method.getMethod().getAnnotation(RequestLog.class);
        logger.info("request log method:{},operation:{},description:{}", signature.getName(), requestLog.logType(), requestLog.description());
        applicationContext.publishEvent(new RequestLogEvent("", requestLog));
    }
}
